#pragma once 
#include <pthread.h>

namespace LockModule
{
    class Mutex
    {
    public:
        //一般而言我们的Mutex的拷贝都需要被禁止，只能通过库中提供的方法初始化 (保证二元信号量的大小是1)
        Mutex(const Mutex&) = delete;
        const Mutex& operator=(const Mutex&) = delete;
        Mutex()
        {
            int n = pthread_mutex_init(&_mutex, nullptr);
            (void)n; // 这里我们对返回值不做处理
        }
        void Lock()
        {
            int n = pthread_mutex_lock(&_mutex);
            (void)n;
        }
        void Unlock()
        {
            int n = pthread_mutex_unlock(&_mutex);
            (void)n;
        }
        pthread_mutex_t* LockPtr() {return &_mutex;}
        ~Mutex()
        {
            int n = pthread_mutex_destroy(&_mutex);
            (void)n;
        }
    private:
        pthread_mutex_t _mutex;
    };

    //我们这里可以提供更加简洁的方法进行加锁
    class LockGuard{
        public:
            LockGuard(Mutex& mutex) :_mutex(mutex)
            {
                _mutex.Lock();
            }
            ~LockGuard()
            {
                _mutex.Unlock();
            }
        private:
            Mutex& _mutex;
    };
}